package com.qsbg.controller;


import com.qsbg.entity.AdminUser;
import com.qsbg.entity.vo.AdminUserVo;
import com.qsbg.entity.vo.ResultVo;
import com.qsbg.mapper.AdminUserMapper;

import com.qsbg.service.impl.AdminUserServiceImpl;
import com.qsbg.utils.Base64Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author LIEFox
 * @since 2021-06-20
 */
@RestController
@RequestMapping("/adminuser")
public class AdminUserController {

    @Autowired
    private AdminUserMapper userMapper;
    @Autowired
    private AdminUserServiceImpl userServiceImpl;

    /**
     * 用户登录接口
     *
     * @param
     * @return
     */
    @PostMapping("/login")
    public ResultVo Login(@RequestBody AdminUserVo adminUserVo) {
        final HashMap<String, Object> map = new HashMap<>();
        map.put("email", adminUserVo.getEmail());
        map.put("password", adminUserVo.getPassword());
        final List<AdminUser> users = userMapper.selectByMap(map);
        System.out.println(users);
        if (users.size() == 1) {
            String token = Base64Utils.encode("email"+123456);
            return ResultVo.ok(token,null);
        } else {
            return ResultVo.fail("用户名或密码错误");
        }

    }


    /**
     * 注册用户
     *
     * @param adminUser
     * @return
     */
    @PostMapping("/sign")
    public String Sign(@RequestBody AdminUser adminUser) {
        try {
            final boolean save = userServiceImpl.save(adminUser);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "fail";
        }
    }


    /**
     * 修改用户密码
     *
     * @param adminUser
     * @return
     */
    @PostMapping("/changepw")
    public String ChangePassWord(@RequestBody AdminUser adminUser) {
        final boolean b = userServiceImpl.updateById(adminUser);
        if (b == true) {
            return "success";
        } else {
            return "fail";
        }
    }


}
